using Microsoft.AspNetCore.Mvc;
using MyHomeworkWebAPi.Models.ef;
using MyHomeworkWebAPi.Models.Request;
using MyHomeworkWebAPi.Models.response;
using MyHomeworkWebAPi.Services;

namespace MyHomeworkWebAPi.Controllers
{
    /// <summary>
    /// 评论管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class CommentsController : ControllerBase
    {
        private readonly ICommentServices _commentServices;

        /// <summary>
        /// 构造函数，注入评论服务
        /// </summary>
        /// <param name="commentServices">评论服务接口</param>
        public CommentsController(ICommentServices commentServices)
        {
            _commentServices = commentServices;
        }

        /// <summary>
        /// 获取所有评论
        /// </summary>
        /// <returns>评论列表</returns>
        [HttpGet]
        public async Task<IActionResult> GetAll()
        {
            var comments = await _commentServices.GetAllAsync();
            return Ok(ApiResponse<List<Comment>>.SuccessResponse(comments));
        }

        /// <summary>
        /// 根据ID获取评论
        /// </summary>
        /// <param name="id">评论ID</param>
        /// <returns>评论详情</returns>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetById(Guid id)
        {
            var comment = await _commentServices.GetByIdAsync(id);
            if (comment == null)
            {
                return NotFound(ApiResponse<Comment>.ErrorResponse("评论不存在", 404));
            }
            return Ok(ApiResponse<Comment>.SuccessResponse(comment));
        }

        /// <summary>
        /// 创建评论
        /// </summary>
        /// <param name="request">评论创建请求</param>
        /// <returns>创建的评论信息</returns>
        [HttpPost]
        public async Task<IActionResult> Create([FromBody] CommentCreateRequest request)
        {
            try
            {
                var comment = await _commentServices.CreateAsync(request);
                return Ok(ApiResponse<Comment>.SuccessResponse(comment, "评论创建成功"));
            }
            catch (Exception ex)
            {
                return BadRequest(ApiResponse<Comment>.ErrorResponse(ex.Message, 400));
            }
        }

        /// <summary>
        /// 删除评论
        /// </summary>
        /// <param name="id">评论ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(Guid id)
        {
            try
            {
                await _commentServices.DeleteAsync(id);
                return Ok(ApiResponse<string>.SuccessResponse("评论删除成功"));
            }
            catch (Exception ex)
            {
                return NotFound(ApiResponse<string>.ErrorResponse(ex.Message, 404));
            }
        }

        /// <summary>
        /// 获取用户的评论列表
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <returns>该用户的评论列表</returns>
        [HttpGet("user/{userId}")]
        public async Task<IActionResult> GetByUserId(Guid userId)
        {
            var comments = await _commentServices.GetByUserIdAsync(userId);
            return Ok(ApiResponse<List<Comment>>.SuccessResponse(comments));
        }

        /// <summary>
        /// 获取商品的评论列表
        /// </summary>
        /// <param name="productId">商品ID</param>
        /// <returns>该商品的评论列表</returns>
        [HttpGet("product/{productId}")]
        public async Task<IActionResult> GetByProductId(Guid productId)
        {
            var comments = await _commentServices.GetByProductIdAsync(productId);
            return Ok(ApiResponse<List<Comment>>.SuccessResponse(comments));
        }

        /// <summary>
        /// 获取评论的回复列表
        /// </summary>
        /// <param name="commentId">评论ID</param>
        /// <returns>该评论的回复列表</returns>
        [HttpGet("{commentId}/replies")]
        public async Task<IActionResult> GetReplies(Guid commentId)
        {
            var replies = await _commentServices.GetRepliesAsync(commentId);
            return Ok(ApiResponse<List<Comment>>.SuccessResponse(replies));
        }

        /// <summary>
        /// 获取评论统计数据
        /// </summary>
        /// <returns>评论统计数据</returns>
        [HttpGet("statistics")]
        public async Task<IActionResult> GetStatistics()
        {
            var statistics = await _commentServices.GetStatisticsAsync();
            return Ok(ApiResponse<CommentStatistics>.SuccessResponse(statistics));
        }
    }
} 